package com.dudu

import android.graphics.Bitmap
import android.graphics.Canvas
import android.graphics.Paint
import androidx.test.platform.app.InstrumentationRegistry
import androidx.test.ext.junit.runners.AndroidJUnit4
import kotlinx.coroutines.delay

import org.junit.Test
import org.junit.runner.RunWith

import org.junit.Assert.*

/**
 * Instrumented test, which will execute on an Android device.
 *
 * See [testing documentation](http://d.android.com/tools/testing).
 */
@RunWith(AndroidJUnit4::class)
class ExampleInstrumentedTest {
    @Test
    fun useAppContext() {
        // Context of the app under test.
        val appContext = InstrumentationRegistry.getInstrumentation().targetContext
        assertEquals("com.dudemo", appContext.packageName)
    }

    @Test
    fun testBitmap(){

        val paint = Paint()
        paint.textSize = 30f
        val bitmap = Bitmap.createBitmap(100, 30, Bitmap.Config.ARGB_8888)
        val bv = Bitmap.createBitmap(100, 30, Bitmap.Config.ARGB_8888)
        val canvas = Canvas(bitmap)
        canvas.drawText("TE", 0f, 25f, paint)

        // 0 - 10
        // 10 - x+y % 10 == 0  0,10,20,30 .. 110
        // 9 - x+y % 9 == 0    9,18,27,36
        // 8 - x+y % 8 == 0    8,16
        // 2 - x+y % 2 == 0    2,4,6,8 ..
        // 1 - x+y % 1 == 0    0,1,2

        val sb = StringBuilder()
        val w = bitmap.width
        val h = bitmap.height

        for (s in 10 downTo  1) {
            sb.clear()
            for (y in 0 until h) {
                for (x in 0 until w) {

                    val bvc = bv.getPixel(x,y)
                    val fvc = bitmap.getPixel(x, y)
                    var vc = bvc
                    if(fvc !=0 && bvc == 0){
                        if(s == 1){
                            bv.setPixel(x,y,fvc)
                            vc = fvc
                        } else if((x+y)%s == 0){
                            bv.setPixel(x,y,fvc)
                            vc = fvc
                        }
                    }

                    if(vc==0)
                        sb.append(" ")
                    else
                        sb.append(".")
                }
                sb.append("\n")
            }
            println(sb.toString())
        }

        assertEquals("11", "11")
    }

}